import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { Answer, AnswerDocument } from './schemas/answer.schema';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';

@Injectable()
export class AnswerService {
  constructor(
    @InjectModel(Answer.name)
    private readonly answerModel: Model<AnswerDocument>,
  ) {}

  // 创建答卷
  async create(answerInfo) {
    try {
      const answer = new this.answerModel(answerInfo);
      return await answer.save();
    } catch (err) {
      throw new HttpException(err, HttpStatus.BAD_REQUEST);
    }
  }
  // 获取答卷列表
  async getAll(id: string) {
    try {
      const res = await this.answerModel.find({ questionId: id }).exec();

      return res;
    } catch (err) {
      throw new HttpException(err, HttpStatus.BAD_REQUEST);
    }
  }

  async count(id: string) {
    try {
      return await this.answerModel.countDocuments({ questionId: id }).exec();
    } catch (err) {
      throw new HttpException(err, HttpStatus.BAD_REQUEST);
    }
  }
}
